/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.*;

/**
 *
 * @author Mickey/Peter
 * this file loads the stored info from the text files into the linked list of farmers for later access to farmer info
 */
@WebServlet(name = "NavigateToLoginPageServlet", urlPatterns = {"/NavigateToLoginPageServlet"})
public class NavigateToLoginPageServlet extends HttpServlet {

    @EJB PreviousPageBean previousPage;
    @EJB AdminBean admin;
    @EJB FarmerBean farmer;
    @EJB SeedBean seed;
    @EJB PostingBean posting;
    @EJB ListOfUsersBean listOfUsers;
    @EJB ListOfSeedsBean listOfSeeds;
    @EJB ListOfPostingsBean listOfPostings;
    
    /**
     * this method loads all the data from the database into the linked list as well as navigating to the login page
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try{
            
            listOfUsers.clear();
            listOfSeeds.clear();
            listOfPostings.clear();
            
            File file = new File("listOfFarmers.txt");
            BufferedReader inputStream;
            String l;
            if (!file.exists()) {
                file.createNewFile();
            }
            else {
                inputStream = new BufferedReader(new FileReader("listOfFarmers.txt"));
                while((l = inputStream.readLine())!= null){
                    farmer = new FarmerBean();
                    farmer.setName(l);
                    farmer.setEmail(inputStream.readLine());
                    farmer.setUserID(inputStream.readLine());
                    farmer.setPassword(inputStream.readLine());
                    farmer.setLoginAttempts(inputStream.readLine());
                    farmer.setIsLocked(inputStream.readLine());
                    listOfUsers.add(farmer);
                }
                inputStream.close();
            }
            file = new File("listOfSeeds.txt");
            if (!file.exists()) {
                file.createNewFile();
            }
            else {
                inputStream = new BufferedReader(new FileReader("listOfSeeds.txt"));
                while((l = inputStream.readLine())!= null){
                    seed = new SeedBean();
                    seed.setCommonName(l);
                    seed.setSciName(inputStream.readLine());
                    seed.setSpecialReq(inputStream.readLine());
                    seed.setGrowingTips(inputStream.readLine());
                    seed.setHarvestInfo(inputStream.readLine());
                    seed.setHardinessZone(inputStream.readLine());
                    seed.setHarvestDay(inputStream.readLine());
                    seed.setHarvestMonth(inputStream.readLine());
                    l = inputStream.readLine();
                    for(int count = 0; count< listOfUsers.size(); count++){
                        if(l.equals(listOfUsers.get(count).getUserID())){
                            farmer = (FarmerBean)listOfUsers.get(count);
                            break;
                        }
                    }
                    seed.setFarmer(farmer);
                    listOfSeeds.add(seed);
                }
                inputStream.close();
            }
            file = new File("listOfPostings.txt");
            if (!file.exists()) {
                file.createNewFile();
            }
            else {
                inputStream = new BufferedReader(new FileReader("listOfPostings.txt"));
                while((l = inputStream.readLine())!= null){
                    posting = new PostingBean();
                    posting.setQuantity(l);
                    posting.setQuantUnit(inputStream.readLine());
                    posting.setDescription(inputStream.readLine());
                    posting.setLocation(inputStream.readLine());
                    posting.setWillShip(inputStream.readLine());
                    posting.setShipCost(Double.parseDouble(inputStream.readLine()));
                    l = inputStream.readLine();
                    for(int count = 0; count< listOfUsers.size(); count++){
                        if(l.equals(listOfUsers.get(count).getUserID())){
                            farmer = (FarmerBean)listOfUsers.get(count);
                            break;
                        }
                    }
                    posting.setFarmer(farmer);
                    l = inputStream.readLine();
                    for(int count = 0; count< listOfUsers.size(); count++){
                        if(l.equals(listOfSeeds.get(count).getSciName())){
                            seed = listOfSeeds.get(count);
                            break;
                        }
                    }
                    posting.setSeed(seed);
                    listOfPostings.add(posting);
                }
                inputStream.close();
            }
            admin = new AdminBean();
            admin.setUserID("admin1");
            admin.setPassword("qualms");
            admin.setName("Addy");
            admin.setEmail("qualms");
            listOfUsers.add(admin);
            previousPage.setNext("login.jsp");
            response.sendRedirect(previousPage.getNext());
        }finally{            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
